package com.starhub.engine.generator.util;

import com.starhub.engine.generator.entity.BasisInfo;
import com.starhub.engine.generator.entity.PropertyInfo;
import com.starhub.engine.generator.entity.ResultJson;

import java.util.List;

/**   
 * <p>说明：  获取文件路径调用创建文件</P>
 * @version: v3.0.0
 * @author: cdy
 */
public class Generator {
	//路径信息
	public static final String ENTITY="entity";
	public static final String DAO="dao";
	public static final String DAO_IMPL="daoImpl";
	public static final String SERVICE="service";
	public static final String SERVICE_IMPL="serviceImpl";
	public static final String BSCONTROLLER ="bsController";
	public static final String APPCONTROLLER="appController";
	public static final String USCONTROLLER="usController";
	public static final String SWAGGER_CONFIG="swaggerConfig";
	public static final String BSENTITY_REQUEST="bsEntityRequest";

	//①创建实体类
	public static ResultJson createEntity(String url, BasisInfo bi) {
		String fileUrl= getGeneratorFileUrl(url, bi.getEntityUrl(), bi.getEntityName(), ENTITY);
		return FreemarkerUtil.createFile(bi, "entity.ftl", fileUrl);
	}

	//①创建请求实体类
	public static ResultJson createBsEntityRequest(String url,BasisInfo bi) {
		String fileUrl= getGeneratorFileUrl(url, bi.getRequestEntityUrl(), bi.getEntityName(), BSENTITY_REQUEST);
		return FreemarkerUtil.createFile(bi, "entityBsRequest.ftl", fileUrl);
	}
	
	//②创建DAO
	public static ResultJson createDao(String url,BasisInfo bi) {
		String fileUrl= getGeneratorFileUrl(url, bi.getDaoUrl(), bi.getEntityName(), DAO);
		return FreemarkerUtil.createFile(bi, "dao.ftl", fileUrl);
	}
	
	//③创建mapper配置文件
	public static ResultJson createDaoImpl(String url,BasisInfo bi) {
		String fileUrl= getGeneratorFileUrl(url, bi.getMapperUrl(), bi.getEntityName(), DAO_IMPL);
		List<PropertyInfo> list=bi.getCis();
		String agile="";
		for (PropertyInfo propertyInfo : list) {
			agile=agile+propertyInfo.getColumn()+", ";
		}
		agile=agile.substring(0, agile.length()-2);
		bi.setAgile(agile);
		return FreemarkerUtil.createFile(bi, "mapper.ftl", fileUrl);
	}
	
	//④创建SERVICE
	public static ResultJson createService(String url,BasisInfo bi) {
		String fileUrl= getGeneratorFileUrl(url, bi.getServiceUrl(), bi.getEntityName(), SERVICE);
		return FreemarkerUtil.createFile(bi, "service.ftl", fileUrl);
	}
	
	//⑤创建SERVICE_IMPL
	public static ResultJson createServiceImpl(String url,BasisInfo bi) {
		String fileUrl= getGeneratorFileUrl(url, bi.getServiceImplUrl(), bi.getEntityName(), SERVICE_IMPL);
		return FreemarkerUtil.createFile(bi, "serviceImpl.ftl", fileUrl);
	}
	
	//⑥创建CONTROLLER
	public static ResultJson createBsController(String url,BasisInfo bi) {
		String fileUrl= getGeneratorFileUrl(url, bi.getBsControllerUrl(), bi.getEntityName(), BSCONTROLLER);
		return FreemarkerUtil.createFile(bi, "bsController.ftl", fileUrl);
	}

	//⑥创建CONTROLLER
	public static ResultJson createAppController(String url,BasisInfo bi) {
		String fileUrl= getGeneratorFileUrl(url, bi.getAppControllerUrl(), bi.getEntityName(), APPCONTROLLER);
		return FreemarkerUtil.createFile(bi, "appController.ftl", fileUrl);
	}

	//⑥创建CONTROLLER
	public static ResultJson createUsController(String url,BasisInfo bi) {
		String fileUrl= getGeneratorFileUrl(url, bi.getUsControllerUrl(), bi.getEntityName(), USCONTROLLER);
		return FreemarkerUtil.createFile(bi, "usController.ftl", fileUrl);
	}


	//⑧创建CONTROLLER
	public static ResultJson createSwaggerConfig(String url,BasisInfo bi) {
		String fileUrl= getGeneratorFileUrl(url,bi.getSwaggerConfigUrl(), "Swagger", SWAGGER_CONFIG);
		return FreemarkerUtil.createFile(bi, "SwaggerConfig.ftl", fileUrl);
	}
	//生成文件路径和名字
	public static String getGeneratorFileUrl(String url,String packageUrl,String entityName, String type){
		if (type.equals("entity")) {
			return url+pageToUrl(packageUrl)+entityName+".java";
		}else if(type.equals("dao")) {
			return url+pageToUrl(packageUrl)+entityName+"Dao.java";
		}else if(type.equals("daoImpl")) {
			return url+pageToUrl(packageUrl)+entityName+"Dao.xml";
		}else if(type.equals("service")) {
			return url+pageToUrl(packageUrl)+entityName+"Service.java";
		}else if(type.equals("serviceImpl")) {
			return url+pageToUrl(packageUrl)+entityName+"ServiceImpl.java";
		}else if(type.equals("bsController")) {
			return url+pageToUrl(packageUrl)+entityName+"ConfigController.java";
		}else if(type.equals("appController")) {
			return url+pageToUrl(packageUrl)+entityName+"AppController.java";
		}else if(type.equals("usController")) {
			return url+pageToUrl(packageUrl)+entityName+"Controller.java";
		}else if(type.equals("bsEntityRequest")){
			return url+pageToUrl(packageUrl)+entityName+"BsRequest.java";
		}
		return null;
	}
	
	public static String pageToUrl(String url) {
		return url.replace(".", "/")+"/";
	}
}
